//
//  MCStoreDetailItemView.swift
//  StoreManage
//
//  Created by MC on 2017/12/26.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCStoreDetailItemView: UIView {

    
    var storeDict = Dictionary<String,Any>() {
        didSet {
            
            // 配送方式
            let openExpress = storeDict["openExpress"] as? NSNumber ?? 0   // 快递送
            let openFlashExpress = storeDict["openFlashExpress"] as? NSNumber ?? 0  // 及时送
            let openInstall = storeDict["openInstall"] as? NSNumber ?? 0   // 到店取
            
            let deliveryArray = NSMutableArray.init()
            if openExpress == 1 {
                deliveryArray.add(#imageLiteral(resourceName: "Order_express"))
            }
            
            if openFlashExpress == 1 {
                deliveryArray.add(#imageLiteral(resourceName: "Order_inTimeToSend"))
            }
            
            if openInstall == 1 {
                deliveryArray.add(#imageLiteral(resourceName: "Order_store"))
            }
            
           
            // 移除三种配送方式 没时间 先这么写着，待优化
            let view1 = self.viewWithTag(1001)
            view1?.removeFromSuperview()
            
            let view2 = self.viewWithTag(1002)
            view2?.removeFromSuperview()
            
            let view3 = self.viewWithTag(1003)
            view3?.removeFromSuperview()
            
            var index = 0
            
            for item in deliveryArray {
                let image = item as? UIImage ?? #imageLiteral(resourceName: "Reuse_placeholder_1_1")
                
                index += 1
                let imageView = UIImageView()
                imageView.tag = 1000 + index;
                imageView.image = image
                self.addSubview(imageView)
                imageView.snp.remakeConstraints { (make) ->Void in
                    make.centerY.equalTo(self)
                    make.size.equalTo(CGSize.init(width: 15, height: 15))
                    make.left.equalTo(titleLabel.snp.right).offset((15 + 5)*index)
                }
            }
        }
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
        
        self.addSubview(arrowImageView)
        arrowImageView.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(self)
            make.right.equalTo(-15)
            make.size.equalTo(CGSize.init(width: 8, height: 14))
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(self)
            make.left.equalTo(15)
        }
        
        self.addSubview(detailLabel)
        detailLabel.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(self)
            make.right.equalTo(arrowImageView.snp.left).offset(-10)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCBlack
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCBlack
        return label
    }()
    
    lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_arrow")
        return imageView
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}
